<template>
  <div class="dashboard-container">
    <common-view
      :table-prop="tableProp"
      :form-rules="formRules"
      module-name="professionConfig"
      :search="getProfessionList"
      :add="addProfession"
      :delete="deleteProfession"
      :update="updateProfession"
      :get-form-data="getFormData"
      :get-search-data="getSearchData"
    />

  </div>
</template>

<script>

import CommonView from '@/components/commonView/index'
import { getProfessionList, addProfession, deleteProfession, updateProfession } from '@/api/profession'
import { getAcademyList } from '@/api/academy'

export default {
  name: 'ProfessionConfig',
  components: { 'common-view': CommonView },
  data() {
    return {
      getProfessionList,
      addProfession,
      deleteProfession,
      updateProfession,
      academyList: [],
      tableProp: [
        {
          prop: 'professionName',
          label: '专业名称'
        },
        {
          prop: 'professionBuildYear',
          label: '设立年份'
        },
        {
          prop: 'academyId',
          label: '学院'
          // formatter: value => {
          //   console.log('value', value, this.academyList)
          //   const findAcademy = this.academyList.find((academy) => academy.id === value.academyId)
          //   return findAcademy ? findAcademy.academyName : '-'
          // }
        },
        {
          prop: 'createTime',
          label: '创建时间'
        }, {
          prop: 'updateTime',
          label: '修改时间'
        }
      ],
      formRules: {
        professionName: [
          { required: true, message: '请输入专业名称', trigger: 'change' }
        ],
        professionBuildYear: [
          { required: true, message: '请选择年份', trigger: 'change' }
        ],
        academyId: [
          { required: true, message: '请选择学院', trigger: 'change' }
        ]

      }
    }
  },
  mounted() {
    // 数据初始化
    this.init()
  },
  methods: {
    async init() {
      const [academyList] = await Promise.all([getAcademyList({})])
      this.academyList = academyList.list
    },
    // 新增、编辑表单转换
    getFormData(formData, handle, id) {
      console.log('转换前', formData)
      const { professionName, professionBuildYear, academyId } = formData
      const newFormData = { professionName, professionBuildYear, academyId }
      if (handle === 'edit') {
        newFormData['professionBuildYear'] = newFormData['professionBuildYear'] + ''
        newFormData['id'] = id
      }
      return newFormData
    },
    // 查询条件转换
    getSearchData(formData) {
      const { professionName, academyName } = formData
      return {
        professionName, academyName
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
}
</style>
